﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Scomponi
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Microsoft.Win32.SaveFileDialog saveFile;
        string path;
        IEnumerable<string> scomposto;
        IEnumerable<string> separators;

        public MainWindow()
        {
            InitializeComponent();

            saveFile = new Microsoft.Win32.SaveFileDialog();

        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                confirm.Content = SpezzaContaParole(TextBox.Text);
            }
            catch { }
        }

        private void Browse_Click(object sender, RoutedEventArgs e)
        {

        }

        private void min_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (max != null && min.Value > max.Value)
            {
                //le faccio scendere insieme
                max.Value = min.Value;
            }
            if (minval != null)
            {
                minval.Text = ((int)min.Value).ToString();
            }
        }

        private void max_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (min != null && max.Value < min.Value)
            {
                //le faccio scendere insieme
                min.Value = max.Value;
            }
            if (maxval != null)
            {
                maxval.Text = ((int)max.Value).ToString();
            }
        }

        private int SpezzaContaParole(String testo)
        {
            var tmp = testo.Trim();

            List<string> res = new List<string>();

            for (int i = 0; i < tmp.Length; i++)
            {
                if (tmp.ElementAt(i) == '\\' && i < tmp.Length - 1)
                {
                    if (separators.Contains(tmp.Substring(i, 2)))
                    {
                        res.Add(tmp.Substring(0, i + 2));
                        tmp = tmp.Substring(i + 2);
                        i = 0;
                    }
                }
                else if (separators.Contains(tmp.ElementAt(i).ToString()))
                {
                    res.Add(tmp.Substring(0, i + 1));
                    tmp = tmp.Substring(i + 1);
                    i = 0;
                }
            }
            res.Add(tmp);
            res = res.Where(c => c != null && !c.Equals("")).ToList<string>();

            scomposto = res;

            //scomposto = tmp.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                risultato.Text = "";
                foreach (var item in res)
                {
                    risultato.Text += item + '\n';
                }
            }
            catch (Exception)
            { }



            return scomposto.Count();
        }

        private void SeparatorBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var sep = ",";
            if (SeparatorBox.Text.Equals(""))
            {
                separators_def.Text = "";
                separators = new List<string>();
                return;
            }

            if (SeparatorBox.Text.StartsWith(sep) && !SeparatorBox.Text.StartsWith(sep + sep))
            {
                if (SeparatorBox.Text.Length == 1)
                {
                    separators_def.Text = ",";
                    separators = new[] { "," };
                    return;
                }

                else
                {
                    SeparatorBox.Text = SeparatorBox.Text.Substring(1);
                }
            }
            else if (SeparatorBox.Text.StartsWith(sep + sep))
            {
                SeparatorBox.Text = ",," + SeparatorBox.Text.Substring(SeparatorBox.Text.TakeWhile(x => x == ',').Count());
            }

            var tmp = SeparatorBox.Text.Split(new char[] { sep.First() }).ToList<string>();
            if (tmp.Contains(""))
            {
                tmp.RemoveAll(x => x.Equals(""));
                if (SeparatorBox.Text.Contains(sep + sep + sep))
                {
                    tmp.Add(sep);
                }
            }
            var seps = "";
            if (SeparatorBox.Text.StartsWith(sep + sep) || SeparatorBox.Text.EndsWith(sep + sep) || SeparatorBox.Text.Contains(sep + sep + sep))
            {
                seps += sep + ' ' + sep;
            }

            for (int i = 0; i < tmp.Count; i++)
            {
                seps += (i == 0 ? "" : ", ") + tmp[i];
            }
            separators = tmp;
            separators_def.Text = seps;

            SpezzaContaParole(TextBox.Text);
        }
    }
}
